﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using BrainTechLLC.WPF;

namespace BrainTechLLC.ImageOperatorsNonAForgeDependent
{
    public class BasicResizedBitmapPopulator
    {
        public virtual Bitmap Populate(BasicResizedBitmap item, ICoreDataProvider dataProvider, string fileName, bool maintainAspectRatio = true, BasicBitmapPopulator bitmapPopulator = null, BasicByteArrayPopulator byteArrayPopulator = null, RenderSettings renderSettings = null)
        {
            Bitmap original = item.BitmapOriginal;

            if (original == null)
            {
                if (bitmapPopulator == null)
                    bitmapPopulator = new BasicBitmapPopulator();

                original = bitmapPopulator.Populate(item, dataProvider, fileName, byteArrayPopulator);
                item.BitmapOriginal = item.Bitmap;
                item.BitmapOriginalWidth = item.BitmapWidth;
                item.BitmapOriginalHeight = item.BitmapHeight;
                item.Bitmap = null;
                item.BitmapWidth = 0;
                item.BitmapHeight = 0;
            }
        
            if (renderSettings  == null)
            {
                renderSettings = new RenderSettings() { ForceHighQuality = false, Interpolation = System.Drawing.Drawing2D.InterpolationMode.Default, PixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb, RenderingMethod = InternalRenderingMethod.None, Smoothing = System.Drawing.Drawing2D.SmoothingMode.HighQuality};
            }
            Bitmap populated = original.ResizeBitmap(item.BitmapOriginalWidth, item.BitmapOriginalHeight, item.BitmapMaxWidth, item.BitmapMaxHeight, renderSettings, maintainAspectRatio);
            item.Bitmap = populated;
            item.BitmapWidth = populated == null ? 0 : populated.Width;
            item.BitmapHeight = populated == null ? 0 : populated.Height;
            return populated;
        }

        public virtual Bitmap PopulateIfEmpty(BasicResizedBitmap item, ICoreDataProvider dataProvider, string fileName, bool maintainAspectRatio = true, BasicBitmapPopulator bitmapPopulator = null, BasicByteArrayPopulator byteArrayPopulator = null, RenderSettings renderSettings = null)
        {
            Bitmap populated = item.Bitmap;
            if (populated == null)
            {
                populated = Populate(item, dataProvider, fileName, maintainAspectRatio, bitmapPopulator, byteArrayPopulator, renderSettings);
            }
            return populated;
        }
    }
}
